package com.media.themovieworld.common;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.media.themovieworld.R;
import com.media.themovieworld.activity.MovieDetailActivity;
import com.media.themovieworld.common.fragment.MovieWorldScreen;
import com.media.themovieworld.screen.detail.MovieDetailScreen;
import com.media.themovieworld.screen.main.MainScreen;
import com.media.themovieworld.screen.menu.InfoScreen;
import com.media.themovieworld.screen.menu.SettingScreen;

public class HeaderBarController implements OnClickListener {

	private OnHeaderBarClickListener mListener;
	
	private MovieWorldScreen mFrag;
	private ImageView imgDrawer;
	private ImageView imgPoster;
	private TextView txtTitle;
	private ImageView imgSearch;
	private View vRoot;
	public HeaderBarController(MovieWorldScreen frag){
		mFrag = frag;
	}
	public View onCreateHeaderView(LayoutInflater inflater,
			ViewGroup container){
		if(!isShowHeader()) return null;
		View v = inflater.inflate(R.layout.header, container,false);
		vRoot = v;
		imgDrawer = (ImageView)v.findViewById(R.id.imgDrawer);
		imgPoster = (ImageView)v.findViewById(R.id.poster);
		txtTitle = (TextView)v.findViewById(R.id.tHeader);
		imgSearch = (ImageView)v.findViewById(R.id.imgSearch);
		imgDrawer.setOnClickListener(this);
		imgPoster.setOnClickListener(this);
		imgSearch.setOnClickListener(this);
		configHeaderBar();
		return v;
	}
	
	private boolean isShowHeader(){
		if (mFrag instanceof MainScreen 
				|| mFrag instanceof ListMovieScreen
				|| mFrag instanceof SettingScreen
				|| mFrag instanceof InfoScreen
				|| mFrag instanceof MovieDetailScreen) {
			return true;
		}
		return false;
	}
	
	private void configHeaderBar(){
		txtTitle.setText(mFrag.getTitle());
		if(!(mFrag instanceof MainScreen)){
			imgDrawer.setImageResource(R.drawable.meta__action_back);
			imgSearch.setVisibility(View.GONE);
		}
	}
	
	public void toggleButtonClick(){
		if (mFrag.getActivity() instanceof MovieDetailActivity) {
			(mFrag.getActivity()).finish();
			return;
		}
		if(!(mFrag instanceof MainScreen)){
			mFrag.getActivity().onBackPressed();
			return;
		}
		mFrag.getMainActivity().toggleMenu();
	}
	
	public void setTitle(String title){
		txtTitle.setText(title);
	}
	
	public void setOnHeaderBarClickListener(OnHeaderBarClickListener listener){
		mListener = listener;
	}
	
	public interface OnHeaderBarClickListener{
		public void onSearchButtonClick(View v);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imgDrawer:
			toggleButtonClick();
			break;
		case R.id.poster:
			toggleButtonClick();
			break;
		case R.id.imgSearch:
			if (mListener!=null) {
				mListener.onSearchButtonClick(vRoot);
			}
			break;
		default:
			break;
		}
	}
}
